<?php

namespace support\register;

use app\services\TokenService;
use plugin\admin\app\common\Util;
use plugin\admin\app\model\User;

class MobileCaptcha extends CommonRegister
{
    public function ifExistsThenLogin($data)
    {
        $user = User::where('mobile', $data['mobile'])->first();

        if ($user) {
            return TokenService::generate($user['id']);

        }

        return false;
    }

    public function isExists($data)
    {
        return User::where('mobile', $data['mobile'])->count() > 0;
    }

    public function action($data)
    {
        $currentDatetime = date('Y-m-d H:i:s');

        $id = User::insertGetId([
            'mobile' => $data['mobile'],
            'created_at' => $currentDatetime,
            'join_time' => $currentDatetime
        ]);

        return User::find($id);
    }

}